package org.example.controller;

import jakarta.annotation.Resource;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.example.util.JsonResult;
import org.example.util.ResultTool;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther:Liu
 * @Testname:RocketController
 * @Date:2025/8/25 12:41
 */
@RestController
@RequestMapping("/test")
public class RocketController {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/one")
    public void one() {
        SendResult sendResult = rocketMQTemplate.syncSend("TopicB:sync", "hello world");
        System.out.println(sendResult);
    }

    @GetMapping("/b")
    public JsonResult b(int number, String body) {
        Message<String> message = MessageBuilder.withPayload(body + 1).setHeader("KEYS", number).build();
        TransactionSendResult transactionSendResult = rocketMQTemplate.sendMessageInTransaction("test", "TopicC:transaction", message, body + 2);
        return ResultTool.success(transactionSendResult);
    }
}
